package com.training.admin.web.controller.projectdata;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.training.admin.entity.course.Course;
import com.training.admin.entity.project.ProjectPhase;
import com.training.admin.entity.project.ProjectPhaseCourse;
import com.training.admin.entity.trainer.Trainer;
import com.training.admin.service.DataService;
import com.training.admin.service.ProjectPhaseCourseService;
import com.training.admin.service.ProjectPhaseService;

/**
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/project-data")
public class ProjectCourseController {
	
	private static final Logger logger = LoggerFactory.getLogger(ProjectDataViewController.class);

	@Autowired
	private ProjectPhaseService projectPhaseService;
	
	@Autowired
	private ProjectPhaseCourseService projectPhaseCourseService;
	
	@Autowired
	@Qualifier("dataService")
	private DataService<Trainer> trainerService;
	
	/**
	 * 添加课程查询条件输入
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{projectPhaseId}/course-search", method = RequestMethod.GET)
	@MenuSetting("submenu-project-data-management")
	public ModelAndView searchCourse(@PathVariable("projectPhaseId") Long projectPhaseId) {
	
		ModelAndView modelAndView = new ModelAndView("project-data/project-data-course-search");
		modelAndView.addObject("projectPhaseId", projectPhaseId);
		return modelAndView;
	}
	
	@RequestMapping(value = "/{projectPhaseId}/bind-cources", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @MenuSetting("submenu-project-data-management")
    public @ResponseBody HttpResult onBindCOurse(@PathVariable("projectPhaseId") Long projectPhaseId,
            @RequestParam List<Long> selectCourseIds) {
        HttpResult httpResult = new HttpResult();
        try {
            projectPhaseCourseService.bindCourse(projectPhaseId, selectCourseIds);

    
            httpResult.success().message("绑定课程成功");
    
        } catch (Exception e) {
            httpResult.fail("500").message("绑定课程失败! " + e.getMessage());
            logger.error("#onBindCourse() - bind course fail, exception is [{}]", e);
        }
    
        return httpResult;
    
    }



	/**
	 * 新增课程输入
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{projectPhaseId}/course-create", method = RequestMethod.GET)
	@MenuSetting("submenu-project-data-management")
	public ModelAndView courseCreate(@PathVariable("projectPhaseId") Long projectPhaseId) {
		ModelAndView modelAndView = new ModelAndView("project-data/project-data-course-create");

		ProjectPhase projectPhase = projectPhaseService.load(ProjectPhase.class, projectPhaseId);
		modelAndView.addObject("projectPhase", projectPhase);
	
		return modelAndView;
	}

	/**
	 * 创建提交
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{projectPhaseId}/course-create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	@MenuSetting("submenu-project-data-management")
	public HttpResult onCourseCreate(@PathVariable("projectPhaseId") Long projectPhaseId,
	                                 @RequestParam(name = "trainerId", required = false) Long trainerId,
	                                 @RequestParam(name = "trainerName", required = false) String trainerName,
	                                 @RequestParam(name = "supplierName", required = false) String supplier,
	                                 @RequestParam(name = "trainerDesc", required = false) String trainerDesc,
	                                 Course course) {
	
		HttpResult httpResult = new HttpResult();
	
		try {
		    projectPhaseCourseService.createProjectPaseCourse(trainerId, trainerName, supplier, trainerDesc, projectPhaseId ,course);
			httpResult.success().message("创建成功");
	
		} catch (Exception e) {
			logger.error("onCreate occurs error", e);
			httpResult.fail("500").message("创建失败");
	
		}
	
		return httpResult;
	}

	/**
	 * 删除项目课程
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{projectPhaseCourseId}/course-delete", method = RequestMethod.GET)
	@MenuSetting("submenu-project-data-management")
	public ModelAndView disableCourse(@PathVariable("projectPhaseCourseId") Long projectPhaseCourseId) {
		ModelAndView modelAndView = new ModelAndView("project-data/project-data-course-delete");
		modelAndView.addObject("projectPhaseCourseId", projectPhaseCourseId);
		return modelAndView;
	}

	/**
	 * 删除项目课程提交
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{projectPhaseCourseId}/course-delete", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@MenuSetting("submenu-project-data-management")
	public @ResponseBody HttpResult onDisableCourse(@PathVariable("projectPhaseCourseId") Long projectPhaseCourseId) {
		HttpResult httpResult = new HttpResult();
		
		try {
		    projectPhaseCourseService.deleteProjectPhaseCourse(projectPhaseCourseId);
		    httpResult.success().message("删除项目课程成功");

        } catch (Exception e) {
            logger.error("#onDeleteCoupon - ", e);
            httpResult.fail("500").message("删除项目课程失败");
        }

		return httpResult;

	}
	
	/**
     * 编辑课程输入
     * 
     * @return
     */
    @RequestMapping(value = "/{projectPhaseCourseId}/edit-course", method = RequestMethod.GET)
    public ModelAndView editCourse(@PathVariable("projectPhaseCourseId") Long projectPhaseCourseId) {
        
        ModelAndView modelAndView = new ModelAndView("project-data/project-data-edit-course");
        
        ProjectPhaseCourse projectPhaseCourse = projectPhaseCourseService.load(ProjectPhaseCourse.class, projectPhaseCourseId);
        modelAndView.addObject("projectPhaseCourse", projectPhaseCourse);
        
        modelAndView.addObject("trainer", projectPhaseCourse.getTrainer());
        
        modelAndView.addObject("course", projectPhaseCourse.getCourse());
        
        return modelAndView;
    }
    
    /**
     * 编辑课程提交
     * @param projectPhaseCourseId
     * @param trainerId
     * @return
     */
    @RequestMapping(value = "/{projectPhaseCourseId}/edit-course", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditCourse(@PathVariable("projectPhaseCourseId") Long projectPhaseCourseId,
                                                 @RequestParam Long trainerId, Course course) {
        HttpResult httpResult = new HttpResult();
        try {
            
            projectPhaseCourseService.editCourse(projectPhaseCourseId, trainerId ,course);
            httpResult.success().message("编辑课程成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("编辑课程失败! " + e.getMessage());
            logger.error("#onEditCourse() - edit course fail, exception is [{}]", e);
        }
        
        return httpResult;
    }
	
	/**
     * 绑定讲师查询条件输入
     * 
     * @return
     */
    @RequestMapping(value = "/{projectPhaseCourseId}/bind-trainer", method = RequestMethod.GET)
    public ModelAndView bindTrainer(@PathVariable("projectPhaseCourseId") Long projectPhaseCourseId) {
    
        ModelAndView modelAndView = new ModelAndView("project-data/project-data-trainer-search");
        modelAndView.addObject("projectPhaseCourseId", projectPhaseCourseId);
        return modelAndView;
    }
    
    /**
     * 绑定讲师
     * @param projectPhaseCourseId
     * @param trainerId
     * @return
     */
    @RequestMapping(value = "/{projectPhaseCourseId}/bind-trainer", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onBindTrainer(@PathVariable("projectPhaseCourseId") Long projectPhaseCourseId,
            @RequestParam Long trainerId) {
        HttpResult httpResult = new HttpResult();
        try {
            projectPhaseCourseService.bindTrainer(projectPhaseCourseId, trainerId);
            httpResult.success().message("绑定讲师成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("绑定讲师失败! " + e.getMessage());
            logger.error("#onBindTrainer() - bind trainer fail, exception is [{}]", e);
        }
        
        return httpResult;
    }
	
}
